package com.petmall.controller;

import com.petmall.entity.Admin;
import com.petmall.entity.Message;
import com.petmall.entity.User;
import com.petmall.service.MessageService;
import com.petmall.service.UserService;
import com.petmall.util.SessionAttributes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpSession;

/**
 * Created by pig2guang on 2017/3/28.
 */
@Controller
public class UserController {

    @Autowired
    private UserService userservice;

    @Autowired
    private MessageService messageservice;

    @RequestMapping(value = "/loginCheck")
    public String loginCheck(@ModelAttribute(value = "user") User user, HttpSession httpSession, Model model){
        user = userservice.selectByPassword(user.getUsername(), user.getPassword());
        if(user != null) {
            httpSession.setAttribute(new SessionAttributes().user , user.getId());
            User user_info = userservice.selectByIdObj(httpSession.getAttribute(new SessionAttributes().user));
            Message message = messageservice.getUserLatestMessage(httpSession.getAttribute(new SessionAttributes().user));
            model.addAttribute("user", user_info);
            model.addAttribute("message", message);
            return "my-account";
        }
        else{
            model.addAttribute("admin", new Admin());
            model.addAttribute("user", new User());
            return "login";
        }
    }

    @RequestMapping(value = "/registerCheck")
    public String registerCheck(@ModelAttribute(value = "user") User user, HttpSession httpSession, Model model){
        user = userservice.insertUser(user.getPhone(), user.getPassword());
        if(user != null){
            httpSession.setAttribute(new SessionAttributes().user , user.getId());
            messageservice.addWelcomeMessage(user);
            Message message = messageservice.getUserLatestMessage(httpSession.getAttribute(new SessionAttributes().user));
            model.addAttribute("user", user);
            model.addAttribute("message", message);
            return "my-account";
        }else{
            model.addAttribute("admin", new Admin());
            model.addAttribute("user", new User());
            return "login";
        }
    }

    @RequestMapping(value = "/myAccount")
    public String myAccount( HttpSession httpSession, Model model ){
        if( httpSession.getAttribute( new SessionAttributes().user ) != null ){
            User user = userservice.selectByIdObj(httpSession.getAttribute(new SessionAttributes().user));
            Message message = messageservice.getUserLatestMessage(httpSession.getAttribute(new SessionAttributes().user));
            model.addAttribute("user", user);
            model.addAttribute("message", message);
            return "my-account";
        }else{
            model.addAttribute("admin", new Admin());
            model.addAttribute("user", new User());
            return "login";
        }
    }

    @RequestMapping(value = "/myInfo")
    public ModelAndView myInfo( HttpSession httpSession ){
        return new ModelAndView("my-info").addObject(userservice.selectByIdObj(httpSession.getAttribute(new SessionAttributes().user)));
    }

    @RequestMapping(value = "/updateUserInfo")
    public ModelAndView updateUserInfo(@ModelAttribute(value = "user") User user, HttpSession httpSession ){
        userservice.updateUserInfo(user, httpSession.getAttribute(new SessionAttributes().user));
        return new ModelAndView("my-info").addObject(userservice.selectByIdObj(httpSession.getAttribute(new SessionAttributes().user)));
    }

    @RequestMapping(value = "/loginOut")
    public String loginOut(HttpSession httpSession, Model model){
        if( httpSession.getAttribute( new SessionAttributes().user ) != null ){
            httpSession.removeAttribute(new SessionAttributes().user);
        }
        if ( httpSession.getAttribute( new SessionAttributes().admin ) != null ) {
            httpSession.removeAttribute(new SessionAttributes().admin);
        }
        model.addAttribute("user", new User());
        model.addAttribute("admin", new Admin());
        return "login";
    }

}
